#include<iostream> #include<cstdio> using namespace std; const int N = 2e6 + 50005;
struct tree { int s[2], v, p, siz; void init(int _v, int _p) { s[0] = s[1] = 0; v = _v, p = _p, siz = 1; }//初始化 } tr[N]; int root, nodes[N], tot, poi;//nodes用来回收删除的节点 char s[N];
void pushup(int p) { tr[p].siz = tr[tr[p].s[0]].siz + tr[tr[p].s[1]].siz + 1; }
int build(int l, int r, int p) { int u = nodes[tot--], mid = (l + r) >> 1; tr[u].init((int)s[mid], p); if (l < mid) tr[u].s[0] = build(l, mid - 1, u); if (r > mid) tr[u].s[1] = build(mid + 1, r, u); pushup(u); return u; }
void rotate(int x) { int y = tr[x].p, z = tr[y].p; int k = tr[y].s[1] == x; tr[z].s[tr[z].s[1] == y] = x, tr[x].p = z; tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y; tr[x].s[k ^ 1] = y, tr[y].p = x; pushup(y), pushup(x); }
void splay(int x, int k) { while (tr[x].p != k) { int y = tr[x].p, z = tr[y].p; if (z != k) { if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x); else rotate(y); } rotate(x); } if (!k) root = x; }
int get_k(int k) { int u = root; while (u) { if (tr[tr[u].s[0]].siz >= k) u = tr[u].s[0]; else if (tr[tr[u].s[0]].siz + 1 == k) return u; else k -= tr[tr[u].s[0]].siz + 1, u = tr[u].s[1]; } return -1; }
void dfs(int u) { if (tr[u].s[0]) dfs(tr[u].s[0]); if (tr[u].s[1]) dfs(tr[u].s[1]); nodes[++tot] = u; }
void output(int u) { if (tr[u].s[0]) output(tr[u].s[0]); printf("%c", (char)tr[u].v); if (tr[u].s[1]) output(tr[u].s[1]); }
int main() { for (int i = 1; i < N; i++) nodes[++tot] = i; int t; scanf("%d", &t); s[1] = 0, s[2] = 127; root = build(1, 2, 0); while (t--) { char op[10]; int k, n; scanf("%s", &op); if (op[0] == 'M') { scanf("%d", &k); poi = k; } else if (op[0] == 'I') { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%c", &s[i]); if ((int)s[i] > 126 || (int)s[i] < 32) i--; } int l = get_k(poi + 1), r = get_k(poi + 2); splay(l, 0), splay(r, l); int v = build(1, n, r); tr[r].s[0] = v; pushup(r), pushup(l); } else if (op[0] == 'D') { scanf("%d", &n); int l = get_k(poi + 1), r = get_k(poi + n + 2); splay(l, 0), splay(r, l); dfs(tr[r].s[0]);//回收节点 tr[r].s[0] = 0; pushup(r), pushup(l); } else if (op[0] == 'G') { scanf("%d", &n); int l = get_k(poi + 1), r = get_k(poi + n + 2); splay(l, 0), splay(r, l); output(tr[r].s[0]); printf("\n"); } else if (op[0] == 'P') poi--; else poi++; } return 0; }
|